import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EdubaseSchoolClassApi } from '#/api/edu/base/setting/schoolClass';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate, handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { getSchoolPageSimple } from '#/api/edu/base/school';
import {
  checkOrg,
  getSchoolTeachingUnitSampleList,
} from '#/api/edu/base/schoolTeachingUnit';
import { getAcademicYearPage } from '#/api/edu/base/setting/academicYear';
import { getSchoolYearGradePage } from '#/api/edu/base/setting/schoolYearGrade';
import { getDeptListCondition } from '#/api/system/dept';
import { getAdminUserPageSimplePermission } from '#/api/system/user';
import { DictBadge } from '#/components/dict-badge';
import { DictTag } from '#/components/dict-tag';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showSchool',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'campusDictList',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'periodDictList',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showCampus',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showCollege',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showDepartment',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showFaculty',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showGroup',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'isSchool',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'areaCode',
      label: '行政区划',
      component: 'ApiTreeSelect',
      rules: 'required',
      dependencies: {
        triggerFields: ['isSchool'],
        show: (values) => !values.isSchool,
        componentProps(values) {
          return {
            async api() {
              const data = await getDeptListCondition({ type: 'area' });
              // 确保每个节点都有value字段，与id保持一致
              const processedData = data.map((item) => ({
                ...item,
                value: item.id,
              }));
              return handleTree(processedData);
            },
            key: values.isSchool || 0,
            labelField: 'name',
            valueField: 'id',
            childrenField: 'children',
            placeholder: '请选择行政区划',
            treeDefaultExpandAll: true,
            allowClear: true,
          };
        },
      },
    },
    {
      fieldName: 'schoolId',
      label: '所属学校',
      component: 'ApiPagingSelect',
      rules: 'required',
      dependencies: {
        triggerFields: ['showSchool', 'areaCode'],
        show: (values) => values.showSchool,
        componentProps(values) {
          return {
            allowClear: true,
            key: values.areaCode + values.showSchool || 0,
            async api() {
              return await getSchoolPageSimple(
                {
                  status: 1,
                  schoolCampusType: 0,
                  areaCode: values.areaCode,
                  pageNo: 1,
                  pageSize: 100,
                },
                false,
                false,
              );
            },
            labelField: 'schoolName',
            valueField: 'id',
            placeholder: '请选择学校',
            onChange: async (val, option: any) => {
              if (val) {
                const res = await checkOrg(val);
                values.showCampus = res.showCampus;
                values.showCollege = res.showCollege;
                values.showDepartment = res.showDepartment;
                values.showFaculty = res.showFaculty;
                values.showGroup = res.showGroup;
                values.campusDictList = res.showCampus
                  ? res.children || []
                  : [];
                values.periodDictList = res.periodList || [];
                values.orgId =
                  res.showCampus ||
                  res.showCollege ||
                  res.showDepartment ||
                  res.showFaculty ||
                  res.showGroup
                    ? undefined
                    : val;
              } else {
                values.showCampus = false;
                values.showCollege = false;
                values.showDepartment = false;
                values.showFaculty = false;
                values.showGroup = false;
                values.campusDictList = [];
                values.campusId = undefined;
                values.periodDictList = [];
                values.orgId = undefined;
              }
            },
          };
        },
      },
    },
    {
      fieldName: 'campusId',
      label: '所属校区',
      component: 'Select',
      rules: 'required',
      dependencies: {
        triggerFields: ['showCampus', 'campusDictList'],
        show: (values) => values.showCampus,
        componentProps(values) {
          return {
            allowClear: true,
            key: values.showCampus || false,
            fieldNames: {
              label: 'name',
              value: 'id',
            },
            options: values.campusDictList || [],
            placeholder: '请选择校区',
            onChange: async (val, option: any) => {
              if (val) {
                values.showCollege = option.showCollege;
                values.showDepartment = option.showDepartment;
                values.showFaculty = option.showFaculty;
                values.showGroup = option.showGroup;
                values.periodDictList = option.periodList || [];
                values.orgId =
                  option.showCollege ||
                  option.showDepartment ||
                  option.showFaculty ||
                  option.showGroup
                    ? undefined
                    : val;
              } else {
                values.showCollege = false;
                values.showDepartment = false;
                values.showFaculty = false;
                values.showGroup = false;
                values.orgId = undefined;
              }
            },
          };
        },
      },
    },
    {
      fieldName: 'orgId',
      label: '所属教育单位',
      component: 'ApiTreeSelect',
      rules: 'required',
      dependencies: {
        triggerFields: [
          'schoolId',
          'campusId',
          'showCollege',
          'showDepartment',
          'showFaculty',
          'showGroup',
        ],
        show: (values) =>
          values.showCollege ||
          values.showDepartment ||
          values.showFaculty ||
          values.showGroup,
        componentProps(values) {
          return {
            allowClear: true,
            key: values.showSchool || false,
            async api() {
              if (!values.schoolId || !values.campusId) {
                return [];
              }
              const data = await getSchoolTeachingUnitSampleList(
                {
                  status: 1,
                  areaCode: values.areaCode,
                  schoolId: values.schoolId,
                  campusId: values.campusId,
                },
                false,
                false,
              );
              // 确保每个节点都有value字段，与id保持一致
              const processedData = data.map((item) => ({
                ...item,
                value: item.id, // 确保value字段存在并与id一致
                label: item.name, // 确保parentId字段存在并与parentId一致
              }));
              const tree = handleTree(processedData);
              return tree;
            },

            placeholder: '请选择教育单位',
          };
        },
      },
    },
    {
      fieldName: 'academicYear',
      label: '学年',
      component: 'ApiPagingSelect',
      rules: 'required',
      componentProps(values) {
        return {
          allowClear: true,
          async api() {
            return await getAcademicYearPage(
              { status: 1, pageNo: 1, pageSize: 20 },
              false,
              false,
            );
          },
          labelField: 'academicName',
          valueField: 'academicYear',
          placeholder: '请选择学年',
        };
      },
    },
    {
      fieldName: 'period',
      label: '所属学段',
      component: 'Select',
      rules: 'required',
      componentProps(values) {
        return {
          allowClear: true,
          options: getDictOptions(DICT_TYPE.EDU_PERIOD, 'number').filter(
            (item) =>
              !values.periodDictList ||
              values.periodDictList.length <= 0 ||
              values.periodDictList.includes(item.value),
          ),
          placeholder: '请选择学段',
        };
      },
    },
    {
      fieldName: 'semesterType',
      label: '学期类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_SEMESTER_TYPE, 'number'),
        placeholder: '请选择学期类型',
      },
    },
    {
      fieldName: 'gradeId',
      label: '年级',
      rules: 'required',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['academicYear', 'schoolId', 'campusId', 'period'],
        componentProps(values) {
          return {
            allowClear: true,
            key: values.gradeId || 0,
            async api() {
              if (
                (!values.schoolId && !values.campusId) ||
                values.academicYear === null ||
                values.academicYear === undefined
              ) {
                return [];
              }
              return await getSchoolYearGradePage(
                {
                  status: 1,
                  pageNo: 1,
                  pageSize: 20,
                  academicYear: values.academicYear,
                  schoolId: values.schoolId,
                  campusId: values.campusId,
                  period: values.period,
                },
                false,
                false,
              );
            },
            labelField: 'gradeAlias',
            valueField: 'id',
            placeholder: '请选择年级',
          };
        },
      },
    },
    {
      fieldName: 'sort',
      label: '序号',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        max: 99,
        placeholder: '请输入排序',
      },
    },
    {
      fieldName: 'className',
      label: '班级名称',
      component: 'Input',
      componentProps: {
        placeholder: '如果为空系统将自动根据年级和序号生成班级名称',
      },
    },
    {
      fieldName: 'classAlias',
      label: '班级别名',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入班级别名',
      },
    },
    {
      fieldName: 'classType',
      label: '班级类型',
      component: 'Select',
      rules: 'required',
      componentProps: {
        options: getDictOptions(DICT_TYPE.EDU_CLASS_TYPE, 'number'),
        placeholder:
          '请选择班级类型：1普通班 2实验班 3国际班 4重点班 5虚拟班（走班制）',
      },
    },
    {
      fieldName: 'classCategory',
      label: '班级类别',
      component: 'Select',
      rules: 'required',
      componentProps: {
        options: getDictOptions(DICT_TYPE.EDU_CLASS_CATEGORY, 'number'),
        placeholder: '请选择班级类别：1行政班/2教学班（大学走班制）',
      },
    },
    {
      fieldName: 'capacity',
      label: '班级容量',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入班级容量',
      },
    },
    {
      fieldName: 'studentCount',
      label: '实际学生人数',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入实际学生人数',
      },
    },
    {
      fieldName: 'majorId',
      label: '专业',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请选择专业',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 5 ||
          values.period === 6 ||
          values.period === 7 ||
          values.period === 8,
      },
    },

    {
      fieldName: 'advisorId',
      label: '班主任/辅导员',
      component: 'ApiPagingSelect',
      componentProps(values) {
        return {
          api: getAdminUserPageSimplePermission,
          labelField: 'text',
          valueField: 'id',
          placeholder: '请选择班主任/辅导员',
        };
      },
    },
    {
      fieldName: 'assistantAdvisorId',
      label: '副班主任',
      component: 'ApiPagingSelect',
      componentProps(values) {
        return {
          api: getAdminUserPageSimplePermission,
          labelField: 'text',
          valueField: 'id',
          placeholder: '请选择副班主任',
        };
      },
    },
    {
      fieldName: 'nurseId',
      label: '保育员',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['period'],
        show: (values) => values.period === 1,
        componentProps(values) {
          return {
            api: getAdminUserPageSimplePermission,
            labelField: 'text',
            valueField: 'id',
            placeholder: '请选择保育员',
          };
        },
      },
    },
    {
      fieldName: 'careLevel',
      label: '保育等级（幼儿园）',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入保育等级（幼儿园）',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) => values.period === 1,
      },
    },

    {
      fieldName: 'mentorId',
      label: '学业导师',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 6 || values.period === 7 || values.period === 8,
        componentProps(values) {
          return {
            api: getAdminUserPageSimplePermission,
            labelField: 'text',
            valueField: 'id',
            placeholder: '请选择学业导师',
          };
        },
      },
    },
    {
      fieldName: 'researchDirection',
      label: '研究方向',
      component: 'Input',
      componentProps: {
        placeholder: '请输入研究方向',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 6 || values.period === 7 || values.period === 8,
      },
    },
    {
      fieldName: 'trainingMode',
      label: '教学模式',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.EDU_TRAINING_MODE, 'number'),
        placeholder: '请选择教学模式',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 5 ||
          values.period === 6 ||
          values.period === 7 ||
          values.period === 8,
      },
    },
    {
      fieldName: 'industryCoachId',
      label: '企业导师',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 5 ||
          values.period === 6 ||
          values.period === 7 ||
          values.period === 8,
        componentProps(values) {
          return {
            api: getAdminUserPageSimplePermission,
            labelField: 'text',
            valueField: 'id',
            placeholder: '请选择企业导师',
          };
        },
      },
    },
    {
      fieldName: 'artScience',
      label: '高考分类',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '理科',
            value: 1,
          },
          {
            label: '文科',
            value: 2,
          },
        ],
        placeholder: '请选择高考分类',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) => values.period === 4,
      },
    },
    {
      fieldName: 'isBilingual',
      label: '是否双语教学',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      rules: 'required',
      defaultValue: 1,
      componentProps: {
        options: [
          {
            label: '无效',
            value: 0,
          },
          {
            label: '在用',
            value: 1,
          },
          {
            label: '已毕业',
            value: 2,
          },
          {
            label: '已归档',
            value: 3,
          },
        ],
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'isGraduated',
      label: '是否已毕业',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'graduationDate',
      label: '毕业日期',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
      dependencies: {
        triggerFields: ['isGraduated'],
        show: (values) => values.isGraduated,
      },
    },

    {
      fieldName: 'buildingId',
      label: '楼栋',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入楼栋ID',
      },
    },
    {
      fieldName: 'floor',
      label: '楼层',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入楼层',
      },
    },
    {
      fieldName: 'classroomId',
      label: '教室',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入教室ID（主要上课地点）',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '学年',
      field: 'academicYear',
    },
    {
      label: '学期',
      field: 'semesterType',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.EDU_SEMESTER_TYPE,
          value: val,
        });
      },
    },
    {
      label: '学段',
      field: 'period',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.EDU_PERIOD,
          value: val,
        });
      },
    },
    {
      label: '年级',
      field: 'gradeCode',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.EDU_GRADE,
          value: val,
        });
      },
    },
    {
      label: '班级编码',
      field: 'classCode',
    },
    {
      label: '班级名称',
      field: 'className',
    },
    {
      label: '班级别名',
      field: 'classAlias',
    },

    {
      label: '班级类型',
      field: 'classType',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.EDU_CLASS_TYPE,
          value: val,
        });
      },
    },
    {
      label: '班级类别',
      field: 'classCategory',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.EDU_CLASS_CATEGORY,
          value: val,
        });
      },
    },
    {
      label: '班级容量',
      field: 'capacity',
    },
    {
      label: '实际学生人数',
      field: 'studentCount',
    },
    {
      label: '班主任/辅导员',
      field: 'advisorId',
    },
    {
      label: '副班主任',
      field: 'assistantAdvisorId',
    },
    {
      label: '保育员',
      field: 'nurseId',
      show: (values) => values?.period === 1,
    },
    {
      label: '保育等级',
      field: 'careLevel',
      show: (values) => values?.period === 1,
    },
    {
      label: '学业导师',
      field: 'mentorId',
      show: (values) =>
        values?.period === 5 ||
        values?.period === 6 ||
        values?.period === 7 ||
        values?.period === 8,
    },
    {
      label: '研究方向',
      field: 'researchDirection',
      show: (values) =>
        values?.period === 6 || values?.period === 7 || values?.period === 8,
    },
    {
      label: '教学模式',
      field: 'trainingMode',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.EDU_TRAINING_MODE,
          value: val,
        });
      },
      show: (values) =>
        values?.period === 5 ||
        values?.period === 6 ||
        values?.period === 7 ||
        values?.period === 8,
    },
    {
      label: '企业导师',
      field: 'industryCoachId',
    },
    {
      label: '高考分类',
      field: 'artScience',
      render: (val) => {
        return h(DictTag, {
          options: [
            {
              label: '理科',
              value: 1,
            },
            {
              label: '文科',
              value: 2,
            },
          ],
          value: val,
        });
      },
      show: (values) => values?.period === 4,
    },
    {
      label: '行政区划',
      field: 'areaName',
    },
    {
      label: '所属学校',
      field: 'schoolName',
    },
    {
      label: '所属校区',
      field: 'campusName',
      show: (values) =>
        values?.campusId && values?.schoolId !== values?.campusId,
    },
    {
      label: '学院',
      field: 'collegeName',
      show: (values) => values?.showCollege || values?.collegeId,
    },
    {
      label: '系',
      field: 'departmentName',
      show: (values) => values?.showDepartment || values?.departmentId,
    },
    {
      label: '学部',
      field: 'facultyName',
      show: (values) => values?.showFaculty || values?.facultyId,
    },
    {
      label: '自定义教学组',
      field: 'groupName',
      show: (values) => values?.showGroup || values?.groupId,
    },
    {
      label: '专业',
      field: 'majorName',
      show: (values) =>
        values?.period === 5 ||
        values?.period === 6 ||
        values?.period === 7 ||
        values?.period === 8,
    },
    {
      label: '状态',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          options: [
            {
              label: '无效',
              value: 0,
            },
            {
              label: '在用',
              value: 1,
            },
            {
              label: '已毕业',
              value: 2,
            },
            {
              label: '已归档',
              value: 3,
            },
          ],
          value: val,
        });
      },
    },
    {
      label: '是否已毕业',
      field: 'isGraduated',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '毕业日期',
      field: 'graduationDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
      show: (values) => values?.isGraduated,
    },
    {
      label: '排序',
      field: 'sort',
    },

    {
      label: '是否双语教学',
      field: 'isBilingual',
    },
    {
      label: '楼栋ID',
      field: 'buildingId',
    },
    {
      label: '楼层',
      field: 'floor',
    },
    {
      label: '教室',
      field: 'classroomId',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'showSchool',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'campusDictList',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'periodDictList',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showCampus',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showCollege',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showDepartment',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showFaculty',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'showGroup',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'isSchool',
      component: 'RadioGroup',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'classCode',
      label: '班级编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入班级编码',
      },
    },
    {
      fieldName: 'className',
      label: '班级名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入班级名称',
      },
    },
    {
      fieldName: 'classAlias',
      label: '班级别名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入班级别名',
      },
    },
    {
      fieldName: 'period',
      label: '学段',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [],
        placeholder: '请选择学段',
      },
    },

    {
      fieldName: 'areaCode',
      label: '行政区划',
      component: 'ApiTreeSelect',
      rules: 'required',
      dependencies: {
        triggerFields: ['isSchool'],
        show: (values) => !values.isSchool,
        componentProps(values) {
          return {
            async api() {
              const data = await getDeptListCondition({ type: 'area' });
              // 确保每个节点都有value字段，与id保持一致
              const processedData = data.map((item) => ({
                ...item,
                value: item.id,
              }));
              return handleTree(processedData);
            },
            key: values.isSchool || 0,
            labelField: 'name',
            valueField: 'id',
            childrenField: 'children',
            placeholder: '请选择行政区划',
            treeDefaultExpandAll: true,
            allowClear: true,
          };
        },
      },
    },
    {
      fieldName: 'schoolId',
      label: '所属学校',
      component: 'ApiPagingSelect',
      rules: 'required',
      dependencies: {
        triggerFields: ['showSchool', 'areaCode'],
        show: (values) => values.showSchool,
        componentProps(values) {
          return {
            allowClear: true,
            key: values.areaCode + values.showSchool || 0,
            async api() {
              return await getSchoolPageSimple(
                {
                  status: 1,
                  schoolCampusType: 0,
                  areaCode: values.areaCode,
                  pageNo: 1,
                  pageSize: 100,
                },
                false,
                false,
              );
            },
            labelField: 'schoolName',
            valueField: 'id',
            placeholder: '请选择学校',
            onChange: async (val, option: any) => {
              if (val) {
                const res = await checkOrg(val);
                values.showCampus = res.showCampus;
                values.showCollege = res.showCollege;
                values.showDepartment = res.showDepartment;
                values.showFaculty = res.showFaculty;
                values.showGroup = res.showGroup;
                values.campusDictList = res.showCampus
                  ? res.children || []
                  : [];
                values.periodDictList = res.periodList || [];
                values.orgId =
                  res.showCampus ||
                  res.showCollege ||
                  res.showDepartment ||
                  res.showFaculty ||
                  res.showGroup
                    ? undefined
                    : val;
              } else {
                values.showCampus = false;
                values.showCollege = false;
                values.showDepartment = false;
                values.showFaculty = false;
                values.showGroup = false;
                values.campusDictList = [];
                values.campusId = undefined;
                values.periodDictList = [];
                values.orgId = undefined;
              }
            },
          };
        },
      },
    },
    {
      fieldName: 'campusId',
      label: '所属校区',
      component: 'Select',
      rules: 'required',
      dependencies: {
        triggerFields: ['showCampus', 'campusDictList'],
        show: (values) => values.showCampus,
        componentProps(values) {
          return {
            allowClear: true,
            key: values.showCampus || false,
            fieldNames: {
              label: 'name',
              value: 'id',
            },
            options: values.campusDictList || [],
            placeholder: '请选择校区',
            onChange: async (val, option: any) => {
              if (val) {
                values.showCollege = option.showCollege;
                values.showDepartment = option.showDepartment;
                values.showFaculty = option.showFaculty;
                values.showGroup = option.showGroup;
                values.periodDictList = option.periodList || [];
                values.orgId =
                  option.showCollege ||
                  option.showDepartment ||
                  option.showFaculty ||
                  option.showGroup
                    ? undefined
                    : val;
              } else {
                values.showCollege = false;
                values.showDepartment = false;
                values.showFaculty = false;
                values.showGroup = false;
                values.orgId = undefined;
              }
            },
          };
        },
      },
    },
    {
      fieldName: 'orgId',
      label: '所属教育单位',
      component: 'ApiTreeSelect',
      rules: 'required',
      dependencies: {
        triggerFields: [
          'schoolId',
          'campusId',
          'showCollege',
          'showDepartment',
          'showFaculty',
          'showGroup',
        ],
        show: (values) =>
          values.showCollege ||
          values.showDepartment ||
          values.showFaculty ||
          values.showGroup,
        componentProps(values) {
          return {
            allowClear: true,
            key: values.showSchool || false,
            async api() {
              if (!values.schoolId || !values.campusId) {
                return [];
              }
              const data = await getSchoolTeachingUnitSampleList(
                {
                  status: 1,
                  areaCode: values.areaCode,
                  schoolId: values.schoolId,
                  campusId: values.campusId,
                },
                false,
                false,
              );
              // 确保每个节点都有value字段，与id保持一致
              const processedData = data.map((item) => ({
                ...item,
                value: item.id, // 确保value字段存在并与id一致
                label: item.name, // 确保parentId字段存在并与parentId一致
              }));
              const tree = handleTree(processedData);
              return tree;
            },

            placeholder: '请选择教育单位',
          };
        },
      },
    },
    {
      fieldName: 'academicYear',
      label: '学年',
      component: 'ApiPagingSelect',
      rules: 'required',
      componentProps(values) {
        return {
          allowClear: true,
          async api() {
            return await getAcademicYearPage(
              { status: 1, pageNo: 1, pageSize: 20 },
              false,
              false,
            );
          },
          labelField: 'academicName',
          valueField: 'academicYear',
          placeholder: '请选择学年',
        };
      },
    },
    {
      fieldName: 'period',
      label: '所属学段',
      component: 'Select',
      rules: 'required',
      componentProps(values) {
        return {
          allowClear: true,
          options: getDictOptions(DICT_TYPE.EDU_PERIOD, 'number').filter(
            (item) =>
              !values.periodDictList ||
              values.periodDictList.length <= 0 ||
              values.periodDictList.includes(item.value),
          ),
          placeholder: '请选择学段',
        };
      },
    },
    {
      fieldName: 'semesterType',
      label: '学期类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_SEMESTER_TYPE, 'number'),
        placeholder: '请选择学期类型',
      },
    },
    {
      fieldName: 'gradeId',
      label: '年级',
      rules: 'required',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['academicYear', 'schoolId', 'campusId', 'period'],
        componentProps(values) {
          return {
            allowClear: true,
            key: values.gradeId || 0,
            async api() {
              if (
                (!values.schoolId && !values.campusId) ||
                values.academicYear === null ||
                values.academicYear === undefined
              ) {
                return [];
              }
              return await getSchoolYearGradePage(
                {
                  status: 1,
                  pageNo: 1,
                  pageSize: 20,
                  academicYear: values.academicYear,
                  schoolId: values.schoolId,
                  campusId: values.campusId,
                  period: values.period,
                },
                false,
                false,
              );
            },
            labelField: 'gradeAlias',
            valueField: 'id',
            placeholder: '请选择年级',
          };
        },
      },
    },
    {
      fieldName: 'classType',
      label: '班级类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_CLASS_TYPE, 'number'),
        placeholder: '请选择班级类型',
      },
    },
    {
      fieldName: 'classCategory',
      label: '班级类别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_CLASS_CATEGORY, 'number'),
        placeholder: '请选择班级类别',
      },
    },
    {
      fieldName: 'advisorId',
      label: '班主任/辅导员',
      component: 'ApiPagingSelect',
      componentProps(values) {
        return {
          api: getAdminUserPageSimplePermission,
          labelField: 'text',
          valueField: 'id',
          placeholder: '请选择班主任/辅导员',
        };
      },
    },
    {
      fieldName: 'assistantAdvisorId',
      label: '副班主任',
      component: 'ApiPagingSelect',
      componentProps(values) {
        return {
          api: getAdminUserPageSimplePermission,
          labelField: 'text',
          valueField: 'id',
          placeholder: '请选择副班主任',
        };
      },
    },
    {
      fieldName: 'nurseId',
      label: '保育员',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['period'],
        show: (values) => values.period === 1,
        componentProps(values) {
          return {
            api: getAdminUserPageSimplePermission,
            labelField: 'text',
            valueField: 'id',
            placeholder: '请选择保育员',
          };
        },
      },
    },
    {
      fieldName: 'careLevel',
      label: '保育等级（幼儿园）',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入保育等级（幼儿园）',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) => values.period === 1,
      },
    },

    {
      fieldName: 'mentorId',
      label: '学业导师',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 6 || values.period === 7 || values.period === 8,
        componentProps(values) {
          return {
            api: getAdminUserPageSimplePermission,
            labelField: 'text',
            valueField: 'id',
            placeholder: '请选择学业导师',
          };
        },
      },
    },
    {
      fieldName: 'researchDirection',
      label: '研究方向',
      component: 'Input',
      componentProps: {
        placeholder: '请输入研究方向',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 6 || values.period === 7 || values.period === 8,
      },
    },
    {
      fieldName: 'trainingMode',
      label: '教学模式',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.EDU_TRAINING_MODE, 'number'),
        placeholder: '请选择教学模式',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 5 ||
          values.period === 6 ||
          values.period === 7 ||
          values.period === 8,
      },
    },
    {
      fieldName: 'industryCoachId',
      label: '企业导师',
      component: 'ApiPagingSelect',
      dependencies: {
        triggerFields: ['period'],
        show: (values) =>
          values.period === 5 ||
          values.period === 6 ||
          values.period === 7 ||
          values.period === 8,
        componentProps(values) {
          return {
            api: getAdminUserPageSimplePermission,
            labelField: 'text',
            valueField: 'id',
            placeholder: '请选择企业导师',
          };
        },
      },
    },
    {
      fieldName: 'artScience',
      label: '高考分类',
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '理科',
            value: 1,
          },
          {
            label: '文科',
            value: 2,
          },
        ],
        placeholder: '请选择高考分类',
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) => values.period === 4,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '无效',
            value: 0,
          },
          {
            label: '在用',
            value: 1,
          },
          {
            label: '已毕业',
            value: 2,
          },
          {
            label: '已归档',
            value: 3,
          },
        ],
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'isGraduated',
      label: '是否已毕业',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '否',
            value: 0,
          },
          {
            label: '是',
            value: 1,
          },
        ],
        placeholder: '请选择是否已毕业',
      },
    },
    {
      fieldName: 'isBilingual',
      label: '是否双语教学',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '否',
            value: 0,
          },
          {
            label: '是',
            value: 1,
          },
        ],
        placeholder: '请选择是否双语教学',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EdubaseSchoolClassApi.SchoolClass>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '班级主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'classCode',
      title: '班级编码',
      minWidth: 80,
    },
    {
      field: 'className',
      title: '班级名称',
      minWidth: 120,
    },
    {
      field: 'classAlias',
      title: '班级别名',
      minWidth: 120,
    },
    {
      field: 'classType',
      title: '班级类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_CLASS_TYPE },
      },
    },
    {
      field: 'classCategory',
      title: '班级类别',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_CLASS_CATEGORY },
      },
    },
    {
      field: 'advisorName',
      title: '班主任/辅导员',
      minWidth: 120,
    },
    {
      field: 'capacity',
      title: '班级容量',
      minWidth: 120,
    },
    {
      field: 'studentCount',
      title: '实际学生人数',
      minWidth: 120,
    },
    {
      field: 'period',
      title: '学段',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_PERIOD },
      },
    },
    {
      field: 'gradeId',
      title: '年级id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'gradeCode',
      title: '年级',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_GRADE },
      },
    },
    {
      field: 'academicYear',
      title: '学年',
      minWidth: 120,
    },
    {
      field: 'semesterType',
      title: '学期',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_SEMESTER_TYPE },
      },
    },
    {
      field: 'semesterId',
      title: '学期id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'schoolName',
      title: '所属学校',
      minWidth: 200,
    },
    {
      field: 'campusName',
      title: '校区',
      minWidth: 200,
    },
    {
      field: 'buildingId',
      title: '楼栋ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'floor',
      title: '楼层',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'classroomId',
      title: '教室ID',
      minWidth: 120,
      visible: false,
    },

    {
      field: 'advisorId',
      title: '班主任/辅导员ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'nurseId',
      title: '保育员id，幼儿园',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'careLevel',
      title: '保育等级（幼儿园）',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'assistantAdvisorId',
      title: '副班主任id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'mentorId',
      title: '学业导师id（大学）',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'researchDirection',
      title: '研究方向（大学）',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'trainingMode',
      title: '教学模式',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'industryCoachId',
      title: '企业导师id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'artScience',
      title: '高考分类',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '理科', value: 1 },
            { label: '文科', value: 2 },
          ],
        },
      },
      visible: false,
    },

    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '无效', value: 0 },
            { label: '在用', value: 1 },
            { label: '已毕业', value: 2 },
            { label: '已归档', value: 3 },
          ],
        },
      },
    },
    {
      field: 'isGraduated',
      title: '是否已毕业',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '否', value: 0 },
            { label: '是', value: 1 },
          ],
        },
      },
    },
    {
      field: 'graduationDate',
      title: '毕业日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 120,
    },
    {
      field: 'areaCode',
      title: '行政区划',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'isBilingual',
      title: '是否双语教学',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
